﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LopSinhVienService;
namespace eTMS.Controllers{
public class LopSinhVienControllerBase : CommonController{
	public LopSinhVienControllerBase()
		: base()
	{	
		this.dao = new LopSinhVienServiceClient();
	}
	~LopSinhVienControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private LopSinhVienEntity oLopSinhVien;	
	public LopSinhVienEntity LopSinhVienObject{
		get{
			return oLopSinhVien;
		}
		set{
			oLopSinhVien=value;
		}
	}
	protected LopSinhVienServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSLopSinhVien(false);			
			return _Table;
		}
	}
	public void LoadAllDSLopSinhVien(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLopSinhVien==null)return false;
		bool result=true;
		try{
			oLopSinhVien = dao.SaveRet(oLopSinhVien);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLopSinhVien.Add(oLopSinhVien);
		}
		return result;
	}
	public bool Update(){
		if(oLopSinhVien==null)return false;
		return dao.Update(oLopSinhVien);
	}
	public bool Delete(){
		if(oLopSinhVien==null)return false;
		bool retvalue = dao.Delete(oLopSinhVien);
		if(retvalue){			
			_ListLopSinhVien.Remove(oLopSinhVien);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 LopSinhVien_ID){
		bool retvalue = dao.DeleteByID(LopSinhVien_ID);		
		if(retvalue){
			LopSinhVienEntity oDeleted=FindLopSinhVien(LopSinhVien_ID);
			if(oDeleted!=null){
				_ListLopSinhVien.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLopSinhVien = new LopSinhVienEntity();
	}
    //Copy data from data row in grid to object
    public LopSinhVienEntity MapDataRowToObject(DataRow row){
		LopSinhVienEntity o = new LopSinhVienEntity();
		 if(!row.IsNull("TrangThai"))
			o.TrangThai=ConvertByte(row["TrangThai"]);
		 if(!row.IsNull("Lop_ID"))
			o.Lop_ID=(Int32)(row["Lop_ID"]);
		 if(!row.IsNull("SinhVien_ID"))
			o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 if(!row.IsNull("LopSinhVien_ID"))
		    o.LopSinhVien_ID=(Int32)(row["LopSinhVien_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LopSinhVienEntity o,DataRow row){
		row["TrangThai"]=o.TrangThai;
		row["Lop_ID"]=o.Lop_ID;
		row["SinhVien_ID"]=o.SinhVien_ID;
		row["LopSinhVien_ID"]=o.LopSinhVien_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LopSinhVienObject,row);
    }
	public void LoadObject(Int32 LopSinhVien_ID){
		LopSinhVienEntity oLoad=FindLopSinhVien(LopSinhVien_ID);
		if(oLoad==null){
			oLopSinhVien=this.LoadLopSinhVien(LopSinhVien_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLopSinhVien.IsNullLopSinhVien_ID;
			if(!bNotIsNull){
				this.ListLopSinhVien.Add(oLopSinhVien);
			}
		}
		else{
			oLopSinhVien=oLoad;
		}
	}
	#region CachedList LopSinhVien
	private static List<LopSinhVienEntity> _ListLopSinhVien=new List<LopSinhVienEntity>();
    public List<LopSinhVienEntity>ListLopSinhVien{
		get{
			return _ListLopSinhVien;
		}
	}
	public static List<LopSinhVienEntity>ListLopSinhVienStatic{
		get{
			return _ListLopSinhVien;
		}
	}
	public LopSinhVienEntity FindLopSinhVien(Int32 LopSinhVien_ID){
		LopSinhVienEntity oResult=this.ListLopSinhVien.Find(delegate(LopSinhVienEntity oFind){
			bool result=true; 
			result&=oFind.LopSinhVien_ID.Equals(LopSinhVien_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLopSinhVien(bool clearCache){
		if(_ListLopSinhVien.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LopSinhVienServiceClient dao = new LopSinhVienServiceClient();
		_ListLopSinhVien=new List<LopSinhVienEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public LopSinhVienEntity LoadLopSinhVien(Int32 LopSinhVien_ID){			
		LopSinhVienEntity o=dao.SelectOne(LopSinhVien_ID);		
		return o;
	}
	#endregion
}
}
